<?php ob_start();
if (!isset($_SESSION)) {
  session_start();
  if(!isset($_SESSION['MM_Username']))header("location:../../index.php");
}
?>
<?php 
	require_once('../../Connections/microassur.php'); 
	require_once('../../utils/fonctions.php');	
	$message = '';
?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

$montantARembourser = '';
if(isset($_GET['Identifiant_contrat']) && isset($_GET['Identifiant_cotisation'])){
	// Fournir le montant a rembourser (première échéance réglée)
	$montantARembourser = montantARembourser($database_microassur, $microassur, GetSQLValueString($_GET['Identifiant_cotisation'], "int"));
}

if(isset($montantARembourser) && isset($_POST['Montant_remboursement'])){
	$compareMontant = strcmp($montantARembourser, $_POST['Montant_remboursement']);
}

if(isset($_POST['Montant_remboursement']) && empty($_POST['Montant_remboursement'])){
	$message = colorer("Vous devez saisir le montant du remboursement.", "false");
}elseif(isset($_POST['Montant_remboursement']) && !verifNumeric($_POST['Montant_remboursement'])){
	$message = colorer("Le montant du remboursement doit être numérique.", "false");
}elseif(isset($montantARembourser) && isset($compareMontant) && $compareMontant!=0){
	$message = colorer('Le montant du remboursement doit être égal au montant de la cotisation réglée lors de la souscription.', 'false');
}elseif(isset($_POST['Date_evenement']) && empty($_POST['Date_evenement'])){
	$message = colorer("Vous devez saisir la date du règlement.", "false");
}elseif(!empty($_POST['Date_evenement']) && !datevalider($_POST['Date_evenement'])){
	$message = colorer("Le format de la date du règlement est JJ-MM-SSAA, JJ étant le jour, MM le mois, SS le siècle, AA l’année.", "false");
}elseif(!empty($_POST['Date_evenement']) && !datevalide($_POST['Date_evenement'])){
	$message = colorer("La date du règlement est incohérente.", "false");
}elseif(isset($_POST['Refenrence_reglement']) && empty($_POST['Refenrence_reglement'])){
	$message = colorer("Vous devez saisir la référence du règlement.", "false");
}elseif(isset($_POST['Refenrence_reglement']) && !veriflibelle1('Refenrence_reglement')){
	$message = colorer("La référence du règlement saisie comporte des caractères spéciaux interdits.","false");
}
//*****************
else{
	if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
	  $insertSQL = sprintf("INSERT INTO evenement (Identifiant_evenement, Type_evenement, Sous_type_evenement, Motif_evenement, Reference_evenement, Date_evenement, Identifiant_agent, Identifiant_sinistre, Identifiant_contrat, Identifiant_personne, Identifiant_echeance, Identifiant_prestation) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)",
						   GetSQLValueString($_POST['Identifiant_evenement'], "int"),
						   GetSQLValueString($_POST['Type_evenement'], "text"),
						   GetSQLValueString($_POST['Sous_type_evenement'], "text"),
						   GetSQLValueString($_POST['Motif_evenement'], "text"),
						   GetSQLValueString($_POST['Mode_reglement'] . $_POST['Refenrence_reglement'], "text"),
						   GetSQLValueString(dateEN2FR($_POST['Date_evenement']), "date"),
						   GetSQLValueString($_POST['Identifiant_agent'], "int"),
						   GetSQLValueString($_POST['Identifiant_sinistre'], "int"),
						   GetSQLValueString($_POST['Identifiant_contrat'], "int"),
						   GetSQLValueString($_POST['Identifiant_personne'], "int"),
						   GetSQLValueString($_POST['Identifiant_echeance'], "int"),
						   GetSQLValueString($_POST['Identifiant_prestation'], "int"));
						   	
	  mysql_select_db($database_microassur, $microassur);
	  mysql_query("START TRANSACTION");
	  $Result1 = mysql_query($insertSQL, $microassur) or die(mysql_error());
	  if($Result1){ 
			$updateSQL3 = sprintf("UPDATE echeancier SET Etat_echeance='NA' WHERE Identifiant_cotisation=%s",
									GetSQLValueString($_GET['Identifiant_cotisation'], "int"));
			$Result3 = mysql_query($updateSQL3, $microassur) or die(mysql_error());
			if($updateSQL3){
				$message = colorer('', 'true'); //$message = colorer('Les échéances suivantes sont passée à l’état NA (non appelée).', 'true');
			}else{
				$message = colorer('Problème modification échéance suivante, renonciation, contactez la DSI.', 'false');
			}
			$updateSQL2 = sprintf("UPDATE echeancier SET Etat_echeance='RM' WHERE Identifiant_cotisation=%s ORDER BY Identifiant_echeance ASC LIMIT 1",
									GetSQLValueString($_GET['Identifiant_cotisation'], "int"));
			$Result2 = mysql_query($updateSQL2, $microassur) or die(mysql_error());
			if($updateSQL2){
				$message = colorer('', 'true'); //$message = colorer('la première échéance est passée à l’état RM (remboursée).', 'true');
			}else{
				$message = colorer('Problème modification première échéance, renonciation, contactez la DSI', 'false');
			}
			mysql_query("commit");
			// $deleteGoTo = "../../menu.php";
			// header(sprintf("Location: %s", $deleteGoTo));
            $message = colorer('Le remboursement de la cotisation est enregistré', 'true');
            unset($_POST) ;
	  }else{
	  	  mysql_query("rollback");
		  $message = colorer('Problème création évènement, renonciation, contacter la DSI.', 'false');
	  }
	}
}


// Dictionnaire
$query_rs_dico1 = 'SELECT Identifiant_table FROM dictionnaire where Code_table="M0006"';
$rs_dico1 = mysql_query($query_rs_dico1, $microassur) or die(mysql_error());
$row_rs_dico1 = mysql_fetch_assoc($rs_dico1);
$num_row = $row_rs_dico1['Identifiant_table'];

// Elements dictionnaire rattachés
$query_rs_dico2 = "SELECT Code_element, Nom_element FROM elementdico where Identifiant_table = '$num_row'";
$rs_dico2 = mysql_query($query_rs_dico2, $microassur) or die(mysql_error());
$row_rs_dico2 = mysql_fetch_assoc($rs_dico2);

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
		<meta http-equiv="X-UA-Compatible" content="IE=8" />
			<title>Remboursement cotisation</title>
			<link rel="stylesheet" type="text/css" href="../../css/main.css" />
            <link rel="stylesheet" type="text/css" href="list.css" />
			<!--[if IE 7]>
            	<link rel="stylesheet" type="text/css" href="../../css/ie7.css" />
            	<link rel="stylesheet" type="text/css" href="form_ie7.css" />
            <![endif]-->
            <link type="text/css" href="../../css/custom-theme/jquery-ui-1.8.11.custom.css" rel="stylesheet" />	
            <script type="text/javascript" src="../../js/jquery-1.5.1.min.js"></script>
            <script type="text/javascript" src="../../js/jquery-ui-1.8.11.custom.min.js"></script>
            <script type="text/javascript" src="../../js/jquery.ui.datepicker-fr.js"></script>
            <script type="text/javascript" src="../../js/scripts.js"></script>
	</head>

<body>
<div id="sf_menu1">
	<h3>Production</h3>
<?php
	if(isset($message) && !empty($message)){
		echo $message;
	}
	$nommodule="Résiliation contrat";
	$nom_module_saisie="Remboursement";
?>
</div>
<div class="cleared"></div>

<div class="corps1">

<form action="<?php echo $editFormAction; ?>" method="post" name="form1" id="form1">
	<div class="imgleft"></div>
	<div class="imgmiddle">
    <div class="btn">
    	<span class="btnleft"></span>
        <span class="btnmiddle">
        	<h5><?php echo $nommodule; ?></h5>
        </span><span class="btnright"></span>    </div>
  	<div class="btn1">
    	<span class="btnleft"></span>
        <span class="btnmiddle">
        	<h5><?php echo $nom_module_saisie; ?></h5>
        </span>
        <span class="btnright"></span>    </div>
	<div class="formulaire">
  <table class="tabform">
    <tr>
      <td>MONTANT A REMBOURSER</td>
      <td><input class="resize_2" type="text" name="Montant_a_rembourser" value="<?php if(isset($montantARembourser)){ echo $montantARembourser; } ?>" size="32" disabled="disabled" /></td>
    </tr>
    <tr>
      <td>MONTANT DU REMBOURSEMENT</td>
      <td><input class="resize_2" type="text" name="Montant_remboursement" value="<?php echo remind_text('Montant_remboursement'); ?>" size="32" maxlength="10" /></td>
    </tr>
    <tr>
      <td>DATE DU REGLEMENT</td>
      <td><input class="resize_2 dateFo" type="text" name="Date_evenement" value="<?php echo remind_text('Date_evenement'); ?>" size="32" maxlength="10" /></td>
    </tr>
    <tr>
      <td>MODE DE REGLEMENT</td>
      <td>
		<select class="resize_1" name="Mode_reglement">
			<?php do { ?>
				<option value="<?php echo $row_rs_dico2['Code_element']; ?>" <?php echo remind_select('Mode_reglement', $row_rs_dico2['Code_element']) ?> >
					<?php echo $row_rs_dico2['Nom_element']; ?>
				</option>
			<?php } while($row_rs_dico2 = mysql_fetch_assoc($rs_dico2)); ?>
		</select>
      </td>
    </tr>
    <tr>
      <td>REFERENCE DU REGLEMENT</td>
      <td><input class="resize_2" type="text" name="Refenrence_reglement" value="<?php echo remind_text('Refenrence_reglement'); ?>" size="32" maxlength="29" /></td>
    </tr>
  </table>
    </div>
</div>
<div class="imgright"></div>				
<div class="cleared"></div>

<div class="bt1">
	<a href="../../menu.php">
		<span class="btleft"></span>
    	<span class="btmiddle">Retour</span>
    	<span class="btright"></span>
    </a>
</div>
<div class="bt">
	<span class="btleft"></span>
    <input type="submit" name="Valider" value="Valider" class="resizeall btmiddle"/>
    <span class="btright"></span>
</div>
<div class="cleared"></div>

  <input type="hidden" name="Identifiant_evenement" value="" />
  <input type="hidden" name="Type_evenement" value="F" />
  <input type="hidden" name="Sous_type_evenement" value="FRC" />
  <input type="hidden" name="Motif_evenement" value="" />
  <input type="hidden" name="Reference_evenement" value="" />
  <input type="hidden" name="Identifiant_agent" value="" />
  <input type="hidden" name="Identifiant_sinistre" value="" />
  <input type="hidden" name="Identifiant_contrat" value="<?php echo $_GET['Identifiant_contrat']; ?>" />
  <input type="hidden" name="Identifiant_personne" value="" />
  <input type="hidden" name="Identifiant_echeance" value="" />
  <input type="hidden" name="Identifiant_prestation" value="" />
  <input type="hidden" name="MM_insert" value="form1" />
</form>
 </div>

</body>
</html>
<?php ob_end_flush(); ?>